<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #canvas1 {
            position: absolute;
            background: black;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
        }
    </style>
</head>
<body>
    <canvas id="canvas1"></canvas>





    <script>
        const canvas = document.getElementById('canvas1');
        const ctx = canvas.getContext('2d');
        // console.log(ctx);
        const ParticleArray = [];

        let hue = 0;

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        window.addEventListener('resize', ()=>{
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;

        })

        const mouse = {
            y: null,
            x: null
        }

        canvas.addEventListener('click' ,(e)=>{
            mouse.x = e.x
            mouse.y = e.y
            for(let i=0; i<5 ;i++){
                ParticleArray.push(new Particle());
            }
            // console.log(e);
            // drawCircle()
        })
        canvas.addEventListener('mousemove' ,(e)=>{
            mouse.x = e.x
            mouse.y = e.y
            for(let i=0; i<3 ;i++){
                ParticleArray.push(new Particle());
            }
            // console.log(e);
            // drawCircle()
        })

        // function drawCircle(){
        //     ctx.fillStyle = 'blue';
        //     ctx.beginPath();
        //     ctx.arc(mouse.x , mouse.y , 50 ,0 ,Math.PI * 2);
        //     ctx.fill();
        // }


        class Particle{
            constructor(){
                this.x = mouse.x;
                this.y = mouse.y; 
                this.size = Math.random() * 15 + 1;
                this.speedX = Math.random() * 3 - 1.5;
                this.speedY = Math.random() * 3 - 1.5;
                this.color = 'hsl(' + hue + ', 100%,50%)';
            }
            update(){
                this.x += this.speedX;
                this.y += this.speedY;
                if(this.size > 0.2) this.size -= 0.1;
            }

            draw(){
                ctx.fillStyle = this.color;
                ctx.beginPath();
                ctx.arc(this.x , this.y , this.size ,0 ,Math.PI * 2);
                ctx.fill();
            }
        }

        // 初始化
        // function init(){
        //     for(let i = 0; i < 100; i++){
        //         ParticleArray.push(new Particle());
        //     }
        // }
        // init()

        // console.log(ParticleArray);
        // 处理获取数组
        function handleParticles(){
            for(let i = 0;i < ParticleArray.length; i++){
                ParticleArray[i].update();
                ParticleArray[i].draw();

                
                for(let j = i; j < ParticleArray.length; j++){
                    const dx = ParticleArray[i].x - ParticleArray[j].x;
                    const dy = ParticleArray[i].y - ParticleArray[j].y;
                    const distance = Math.sqrt(dx * dx + dy * dy) ; 
                    if(distance < 100){
                        ctx.beginPath();
                        ctx.strokeStyle = ParticleArray[i].color;
                        ctx.lineWidth = 0.2;
                        ctx.moveTo(ParticleArray[i].x, ParticleArray[i].y)
                        ctx.lineTo(ParticleArray[j].x, ParticleArray[j].y)
                        ctx.stroke();
                        ctx.closePath();
                    }  
                }
                if(ParticleArray[i].size <= 0.3){
                    ParticleArray.splice(i, 1)
                    // console.log(ParticleArray.length);
                    i--;
                }
            }
        }

        function animate(){
            ctx.clearRect(0,0, canvas.width, canvas.height);
            // drawCircle();

            // ctx.fillStyle = 'rgba(0,0,0,0.01)'; 这和123行是不一样的
            // ctx.fillRect(0,0, canvas.width, canvas.height)

            handleParticles()
            hue +=2; //色彩会变化更快
            requestAnimationFrame(animate)
        }

        animate()
    </script>
</body>
</html>